package com.mall.manager.authority.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.util.WebUtils;

import com.mall.untils.Page;
import com.mall.untils.StringUtil;

/**
 * 角色信息列表
 * @author liugb
 * @date 2016 7 7
 */
@Service
public class PartService {
	@Resource
    private JdbcTemplate jdbcTemplate;
	//日志信息
	private Logger logger = LoggerFactory.getLogger(PartService.class);
	
	/**
	 * 用户列表分页方法
	 * @param request
	 * @return
	 */
	public Page manager_tpart_list(HttpServletRequest request) {
		Page page = null;
	    Map<String,Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    int pageNo = MapUtils.getIntValue(paraMap, "currentpageno");
	    String name = MapUtils.getString(paraMap, "name");
	    String str = "";
	    if(!StringUtils.isEmpty(name)){
	    	str = StringUtil.getByteToString(name);  	    	
	    }
	    int pageSize = 10;
    	try {
 	        if (pageNo == 0){
 	        	pageNo = 1;
 	        }
 	       String sql = "";
 	        if(StringUtils.isEmpty(str)){
 	        	sql = "select count(*) from db_report.t_part ";
 	        }else{
 	        	sql = "select count(*) from db_report.t_part where 1=1 and name like '%"+ str +"%'";
 	        }
 	        int total = this.jdbcTemplate.queryForObject(sql, Integer.class);
 	        int startIndex = Page.getStartOfPage(pageNo, pageSize);
 	        if(StringUtils.isEmpty(str)){
 	        	sql = "select * from db_report.t_part order by id desc limit " + startIndex + "," + pageSize; 	
 	        }else{
 	        	sql = "select * from db_report.t_part where 1=1 and name like '%"+ str +"%' order by id desc limit " + startIndex + "," + pageSize;
 	        }
 	       List<Map<String, Object>>  queryForList = this.jdbcTemplate.queryForList(sql);
 	        page = new Page(startIndex, total, pageSize, queryForList);
 	        return page;
 	    } catch (Exception e) {
 	        this.logger.info(e.toString());
 	        return null;
 	    }
	}
	/**
	 * 角色删除方法
	 * @param paraMap
	 * @return
	 */
	public int manager_del_tpart(Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "id");
		try {
			String sql="delete from t_part where id=?";
			int update = jdbcTemplate.update(sql,id);
			return update;
		} catch (Exception e) {
			return 0;
		}
	}
	/**
	 * 角色修改方法
	 * @param paraMap
	 * @return
	 */
	public int manager_update_tpart(Map<String, Object> paraMap) {
	
		String name = MapUtils.getString(paraMap, "name");
		String remarks = MapUtils.getString(paraMap, "remarks");
		String id = MapUtils.getString(paraMap, "id");
		try {
			String sql = "update t_part set name=?,remarks=? where id = ?";
			int update = jdbcTemplate.update(sql,name,remarks,id);
			return update;
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.toString());
			return 0;
		}
	}
    /**
     * 角色信息添加数据
     * @param paraMap
     * @return
     */
	public int manager_insert_tpart(Map<String, Object> paraMap) {
		String name = MapUtils.getString(paraMap, "name");
		String remarks = MapUtils.getString(paraMap, "remarks");
		try {
			String sql="insert into t_part(name,remarks,create_time,update_time) values(?,?,now(),now())";
			int update = jdbcTemplate.update(sql,name,remarks);
			return update;
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
			return 0;
		}
	}
	
	
	public List<Map<String, Object>> findAll(){
		String sql="select * from  t_part";
		try {
			return jdbcTemplate.queryForList(sql);
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.toString());
		}
		
		return null;
	}
	/**
	 * 根据id查询
	 * @param userId
	 * @return
	 */
	public Map<String, Object> findById(Integer partId){
		String sql = "select * from t_part where id=?";
		Map<String, Object> map = jdbcTemplate.queryForMap(sql, partId);
		return map;
	}
	
}
